/*=========================================================================

Program:   Virtual Surgery
Language:  C++
Date:      2009-9-1
Version:   0.1
Mail:		   nelfylee@gmail.com

Copyright (c) 2009 Nelfy Lee. All rights reserved.

=========================================================================*/
#include "View.h"

#include "vtkActor2D.h"
#include "vtkEventQtSlotConnect.h"
#include "vtkInteractorStyleImage.h"
#include "vtkRenderWindow.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkPolyDataMapper.h"
#include "vtkRectangularButtonSource.h"


#include <QLabel>

View::View(const QString &name,int id)
{
	m_id = id;
	m_maximized = false;
	m_lbtnPressed = false;
	m_activated = false;

	m_viewFrame = new QFrame(this);
	m_qvtkWidget = new QVTKWidget(m_viewFrame);

	QHBoxLayout *layout = new QHBoxLayout(m_viewFrame);
	layout->setMargin(0);
	layout->setSpacing(0);
	layout->addWidget(m_qvtkWidget);
	
	m_renderer = vtkRenderer::New();
	m_qvtkWidget->GetRenderWindow()->AddRenderer(m_renderer);

	m_labelTitle = new QLabel(name);
	m_layoutToolbar = new QHBoxLayout;
	m_layoutToolbar->setSpacing(0);
	m_layoutToolbar->setMargin(0);
	m_layoutToolbar->addWidget(m_labelTitle);
	m_layoutToolbar->addStretch();


	QHBoxLayout *layoutHeader = new QHBoxLayout;

	m_btnResetView = new QToolButton;
	m_btnResetView->setIcon(QIcon(QPixmap(":/res/bw_resetview.png")));
	m_btnResetView->setAutoRaise(true);


	m_btnCapture = new QToolButton;
	m_btnCapture->setIcon(QIcon(QPixmap(":/res/bw_digitalcam.png")));
	//m_btnCapture->setIconSize(QSize(16, 16));
	m_btnCapture->setAutoRaise(true);

	m_btnMaximize = new QToolButton;
	m_btnMaximize->setIcon(QIcon(QPixmap(":/res/bw_window_max.png")));
	//m_btnMaximize->setIconSize(QSize(16, 16));
	m_btnMaximize->setAutoRaise(true);


	connect(m_btnMaximize, SIGNAL(clicked()), this, SLOT(toggleMaximize()));

	layoutHeader->addLayout(m_layoutToolbar);
	layoutHeader->addWidget(m_btnCapture);
	layoutHeader->addWidget(m_btnResetView);
	layoutHeader->addWidget(m_btnMaximize);	

	QVBoxLayout *layoutmain = new QVBoxLayout(this);
	layoutmain->setMargin(0);
	layoutmain->setSpacing(0);
	layoutmain->addLayout(layoutHeader);
	layoutmain->addWidget(m_viewFrame);

	setLayout(layoutmain);
	setFrameStyle(Sunken | StyledPanel);

// 	vtkRectangularButtonSource *button = vtkRectangularButtonSource::New();
// 	button->SetWidth(0.2);
// 	button->SetHeight(0.2);
// 	//button->SetDepth(0.2);
// 	button->SetCenter(0,1,0);
// 	//button->SetTextureDimensions();
// 
// 	vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
// 	mapper->SetInputConnection(button->GetOutputPort());
// 
// 	vtkActor *actor = vtkActor::New();
// 	actor->SetMapper(mapper);
// 
// 	m_renderer->AddActor(actor);

	resetBackground();
}


void View::addProper(vtkProp *prop)
{
	m_outPropList.push_back(prop);

	m_renderer->AddViewProp(prop);
}


void View::addProper2D(vtkActor2D *prop)
{
	m_outPropList.push_back(prop);

	m_renderer->AddActor2D(prop);
}


void View::update()
{
	resetCamera();
	render();
}


void View::selectProper(vtkProp *prop)
{

}


void View::removeProper(vtkProp *prop)
{
	QList<vtkProp*>::iterator it = m_outPropList.begin();
	for ( ; it != m_outPropList.end(); it++)
	{
		if ( *it == prop)
		{
			m_outPropList.erase(it);
			m_renderer->RemoveViewProp(prop);
			render();
			return;
		}
	}
}


void View::clearOutsidePropers()
{
	QList<vtkProp*>::iterator it = m_outPropList.begin();
	for ( ; it != m_outPropList.end();)
	{
		m_renderer->RemoveViewProp(*it);
		it = m_outPropList.erase(it);
	}
}


void View::resetBackground()
{
	m_renderer->SetBackground(0,0,0);
}

void View::render()
{
	m_qvtkWidget->GetRenderWindow()->Render();
	//m_renderer->Render();
}

void View::renderAll()
{
	m_renderer->Render();
	m_qvtkWidget->GetRenderWindow()->Render();
	m_qvtkWidget->update();
	m_qvtkWidget->GetInteractor()->Render();
	m_renderer->ResetCamera();
}

void View::resetCamera()
{
	m_renderer->ResetCamera();
}


void View::resizeEvent(QResizeEvent *event)
{
	QFrame::resizeEvent(event);

	if ( m_qvtkWidget)
	{
		int *s = m_qvtkWidget->GetRenderWindow()->GetSize();
		m_size[0] = s[0];
		m_size[1] = s[1];
	}
}


void View::slotLeftButtonPressed(vtkObject *obj, unsigned long vtk_even)
{
	m_lbtnPressed = true;

	if ( m_activated)
		return;

		emit emitLeftClickedMe(m_id);
}


void View::setActivated(bool active) 
{
	m_activated = active;

	if ( active)
	{
		m_viewFrame->setFrameStyle(Plain|Box);
		m_viewFrame->setLineWidth(2);
		m_viewFrame->setStyleSheet(QString::fromUtf8("color: rgb(198, 174, 94);"));
	}
	else
	{
		m_viewFrame->setFrameShape(NoFrame);
	}
}


bool View::isActivated()
{
	return m_activated;
}


void View::captureScreen()
{
/*

//capture screen

int *size = this->renWin->GetSize();
this->renWin->SetupMemoryRendering(size[0], size[1],
GetDC()->GetSafeHdc());
this->renWin->Render();

unsigned char *data = this->ren->GetVTKWindow()->GetPixelData(0, 0,
size[0]-1, size[1]-1, 1);

CDC *pDC = GetDC();
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(pDC, size[0], size[1]);

unsigned long *bits = new unsigned long[size[0] * size[1]];
int i, j, k;
for (j=0; j<size[1]; j++) {
for (i=0; i<size[0]; i++) {
k = 3 * ((size[1] - j - 1) * size[0] + i);
bits[j*size[0]+i] = RGB(data[k+2], data[k+1], data[k]);
}
}
bitmap.SetBitmapBits(size[0] * size[1] * 4, (void *)bits);
delete []bits;
delete []data;

EmptyClipboard();
SetClipboardData(CF_BITMAP, bitmap);
CloseClipboard();
bitmap.DeleteObject();
ReleaseDC(pDC);
this->renWin->ResumeScreenRendering();
this->renWin->Render();*/
}


void View::toggleMaximize()
{
	if ( m_maximized == false)
	{
		emit emitMaxmizeMe(m_id);
		m_btnMaximize->setIcon(QIcon(QPixmap(":/res/bw_window_nomax.png")));
		m_maximized = true;
	}
	else
	{
		emit emitRestoreMe(m_id);
		m_btnMaximize->setIcon(QIcon(QPixmap(":/res/bw_window_max.png")));
		m_maximized = false;
	}
}

void View::slotLeftButtonReleased( vtkObject *, unsigned long )
{
	m_lbtnPressed = false;
}
